package com.bw.shiro.jdbcshiro;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

public class JDBCShiro {
    @Test
    public void testjDBCShiro(){
        //声明一个shiro的核心
        DefaultSecurityManager manager = new DefaultSecurityManager();
        //声明一个阿里的数据源
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:mysql:///w4");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        //声明一个域
        JdbcRealm jdbcRealm = new JdbcRealm();
        jdbcRealm.setDataSource(dataSource);

        //设置使用 的realm
        manager.setRealm(jdbcRealm);
        //安全管理
        SecurityUtils.setSecurityManager(manager);
        //主体
        Subject subject = SecurityUtils.getSubject();
        //前台提交的用户名和密码
        UsernamePasswordToken token = new UsernamePasswordToken("zhangsan","111");
        //login
        subject.login(token);
        //授权验证
        if(subject.isAuthenticated()){
            System.out.println("授权成功："+true);
        }

    }
}
